package com.dhcc.bpm.modules.bbs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.SensitiveUtils;
import com.dhcc.bpm.modules.bbs.entity.Sensitive;
import com.dhcc.bpm.modules.bbs.service.ISensitiveService;
import com.dhcc.bpm.modules.bbs.vo.SensitiveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName SensitiveController
 * @Description 敏感词汇表 控制器
 * @Author daina
 * @Date 2020/4/14 10:24
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/

@RestController
@AllArgsConstructor
@RequestMapping("bpm-bbs/sensitive")
@Api(value = "敏感词汇表", tags = "敏感词汇表接口")
public class SensitiveController extends BladeController {

	private ISensitiveService sensitiveService;
	private SensitiveUtils sensitiveUtils;
	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 10:25 2020/4/14
	 * @param sensitive
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.bbs.entity.Sensitive>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sensitive")
	@ApiLog("查看帖子详情")
	@Log(title = "查看帖子详情",businessType = BusinessType.SELECT)
	public R<Sensitive> detail(Sensitive sensitive) {
		Sensitive detail = sensitiveService.getOne(Condition.getQueryWrapper(sensitive));
		return R.data(detail);
	}

	/**
	 * @Description 分页 敏感词汇表
	 * @Author daina
	 * @Date 11:43 2020/4/14
	 * @param sensitive
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.entity.Sensitive>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sensitive")
	@ApiLog("分页 敏感词汇表")
	@Log(title = "分页 敏感词汇表",businessType = BusinessType.SELECT)
	public R<IPage<Sensitive>> list(Sensitive sensitive, Query query) {

		IPage<Sensitive> pages = sensitiveService.page(Condition.getPage(query), Condition.getQueryWrapper(sensitive));

		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 敏感词汇表
	 * @Author daina
	 * @Date 11:43 2020/4/14
	 * @param sensitive
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.vo.SensitiveVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sensitive")
	@ApiLog("自定义分页 敏感词汇表")
	@Log(title = "自定义分页 敏感词汇表",businessType = BusinessType.SELECT)
	public R<IPage<SensitiveVO>> page(SensitiveVO sensitive, Query query) {
		IPage<SensitiveVO> pages = sensitiveService.selectSensitivePage(Condition.getPage(query), sensitive);
		return R.data(pages);
	}

	/**
	 * @Description 新增 敏感词汇表
	 * @Author daina
	 * @Date 11:44 2020/4/14
	 * @param sensitive
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sensitive")
	@ApiLog("新增 敏感词汇表")
	@Log(title = "新增 敏感词汇表",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody Sensitive sensitive) {
		return R.status(sensitiveService.save(sensitive));
	}

	/**
	 * @Description 修改 敏感词汇表
	 * @Author daina
	 * @Date 11:44 2020/4/14
	 * @param sensitive
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sensitive")
	@ApiLog("查看帖子详情")
	@Log(title = "查看帖子详情",businessType = BusinessType.SELECT)
	public R update(@Valid @RequestBody Sensitive sensitive) {
		return R.status(sensitiveService.updateById(sensitive));
	}

	/**
	 * @Description 新增或修改 敏感词汇表
	 * @Author daina
	 * @Date 11:44 2020/4/14
	 * @param sensitive
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sensitive")
	@ApiLog("新增或修改 敏感词汇表")
	@Log(title = "新增或修改 敏感词汇表",businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody Sensitive sensitive) {
		try {
			sensitive.setSensitiveName(sensitive.getSensitiveName().trim());
			boolean flag = sensitiveService.saveOrUpdate(sensitive);
			if(flag){
				sensitiveUtils.refreshSensitiveWords("开放平台");
			}
			return R.status(flag);
		} catch (Exception e) {
			if (e.getMessage().contains("for key 'opcsensitive_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该敏感词名称已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}

	}

	
	/**
	 * @Description 删除 敏感词汇表
	 * @Author daina
	 * @Date 11:44 2020/4/14
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("删除 敏感词汇表")
	@Log(title = "删除 敏感词汇表",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		boolean flag =sensitiveService.deleteLogic(Func.toLongList(ids));
		if(flag){
			sensitiveUtils.refreshSensitiveWords(")开放平台");
		}
		return R.status(flag);
	}
}
